<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>开始使用 layui</title>
    <link rel="stylesheet" href="./layui/css/layui.css">
    <title>Title</title>
</head>
<body>


<div  id="loginFormDiv">
    <form class="layui-form"  lay-filter="example ">
        <div  class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-inline">
                <input type="text" name="username" required  lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码框</label>
            <div class="layui-input-inline">
                <input type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>
<!--        <div class="layui-form-item">-->
<!--            <label class="layui-form-label">验证码</label>-->
<!--            <div class="layui-input-inline">-->
<!--                <input type="text" name="capchar" autocomplete="off" placeholder="请输入验证码" class="layui-input">-->
<!--            </div>-->
<!--            <div class="layui-input-inline">-->
<!--                <img src="/captcha" style="width:85px;height: 38px" id="cap"/>-->
<!--            </div>-->
<!--        </div>-->
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="loginBut">登录</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>

<script src="./layui/layui.js"></script>
<script>
    //Demo
    layui.use( function(){
        var layer =layui.layer;
        var form = layui.form;
        var $ = layui.$;

        //监听提交
        form.on('submit(loginBut)', function(data){

            $.ajax({
                url:"/login",
                data:data.field,
                type:"post",
                success:function (result) {
                    console.log(result);
                    if(result.code == 1){
                        window.location ="/index.html";
                    }else{
                        layer.alert(result.message);
                    }
                }
            });
            return false;
        });


        $("#cap").click(function(){
            //刷新验证码
            path = $(this).attr("src")+"?"+new Date().getTime();
            $(this).attr("src",path);
        });

        //自定义验证规则
        form.verify({
            title: function(value){
                if(value.length < 4){
                    return '标题至少得4个字符啊';
                }
            }
            ,pass: [
                /^[\S]{5,12}$/
                ,'密码必须5到12位，且不能出现空格'
            ]

        });
    });
</script>
<style>
    #loginFormDiv {
        border: 1px solid blue;
        margin: 100px auto;
        padding: 50px 20px 20px 50px ;
        width: 600px;
        height: 300px;
        box-shadow: 15px 15px 15px 15px #888888;
    }

</style>
</body>
</html>